subroutine print_count_obj(lunit,ounit,ti,dp,sd,lo,te,nout,myrank)
  use param
  use count_param,only:count_obj
  use count_param,only:total_count_obj
  use group_param,only:n_object_group
  use output_param,only:print_spatial_dist
  use count_param,only:spatial_file_name
  use box_param,only:channel_n
  use output_param,only:print_reaction_rate
  use output_param,only:prfn
  use output_param,only:print_all_position
  use output_param,only:ppfn
  use group_param,only:react_group
  use group_param,only:n_react_group
  use table_param,only:n_sim_obj
  implicit none
  
  integer,intent(in) :: lunit,ounit,myrank
  real(dbl),intent(in) :: ti,dp,sd,lo,te,nout

  integer :: i,j,n,nu,total_n_obj
  character(len=len_linebuf) :: str

  nu=1000

  ! Print to the terminal
  if (myrank.eq.0) then
     write(*,'(1pe11.5,1x)',advance='no') te
     write(*,'(1pe11.5,1x)',advance='no') ti
     write(*,'(1pe11.5,1x)',advance='no') dp
     write(*,'(1pe11.5,1x)',advance='no') sd
     write(*,'(i7,1x)') n_sim_obj
  end if

  ! log file
  write(lunit,'(1pe11.5,1x)',advance='no') te
  write(lunit,'(1pe11.5,1x)',advance='no') ti
  write(lunit,'(1pe11.5,1x)',advance='no') dp
  write(lunit,'(1pe11.5,1x)',advance='no') sd
  write(lunit,'(i7,1x)') n_sim_obj

  ! output file
  write(ounit,'(1pe11.5,1x)',advance='no') te
  write(ounit,'(1pe11.5,1x)',advance='no') ti
  write(ounit,'(1pe11.5,1x)',advance='no') dp
  write(ounit,'(1pe11.5,1x)',advance='no') sd
  do i=1,n_object_group
     write(ounit,'(i7,1x)',advance='no') &
          total_count_obj(i)
  end do
  write(ounit,*)

  ! Spatial distribution
  if (print_spatial_dist) then
     do i=1,channel_n(1)
        open(unit=nu,file=spatial_file_name%fnx(i),&
             status='old',access='append')
        write(nu,'(1pe11.5,1x)',advance='no') te
        write(nu,'(1pe11.5,1x)',advance='no') ti
        write(nu,'(1pe11.5,1x)',advance='no') dp
        write(nu,'(1pe11.5,1x)',advance='no') sd
        do j=1,n_object_group
           write(nu,'(i7,1x)',advance='no') &
                count_obj(j)%nx(i)
        end do
        write(nu,*)
        close(nu)
     end do
     do i=1,channel_n(2)
        open(unit=nu,file=spatial_file_name%fny(i),&
             status='old',access='append')
        write(nu,'(1pe11.5,1x)',advance='no') te
        write(nu,'(1pe11.5,1x)',advance='no') ti
        write(nu,'(1pe11.5,1x)',advance='no') dp
        write(nu,'(1pe11.5,1x)',advance='no') sd
        do j=1,n_object_group
           write(nu,'(i7,1x)',advance='no') &
                count_obj(j)%ny(i)
        end do
        write(nu,*)
        close(nu)
     end do
     do i=1,channel_n(3)
        open(unit=nu,file=spatial_file_name%fnz(i),&
             status='old',access='append')
        write(nu,'(1pe11.5,1x)',advance='no') te
        write(nu,'(1pe11.5,1x)',advance='no') ti
        write(nu,'(1pe11.5,1x)',advance='no') dp
        write(nu,'(1pe11.5,1x)',advance='no') sd
        do j=1,n_object_group
           write(nu,'(i7,1x)',advance='no') &
                count_obj(j)%nz(i)
        end do
        write(nu,*)
        close(nu)
     end do
  end if

  if (print_reaction_rate) then
     open(unit=nu,file=prfn,status='old',access='append')
     write(nu,'(1pe11.5,1x)',advance='no') te
     write(nu,'(1pe11.5,1x)',advance='no') ti
     write(nu,'(1pe11.5,1x)',advance='no') dp
     write(nu,'(1pe11.5,1x)',advance='no') sd
     do i=1,n_react_group
        write(nu,'(1pe11.5,1x)',advance='no') &
             react_group(i)%a_count
     end do
     write(nu,*)
     close(nu)
  end if

  if (print_all_position) then
     n=len_trim(ppfn)+11
     write(str(1:n),'(i5.5,".",a,".r",i3.3)') &
          int(nout),trim(ppfn),myrank
     open(unit=nu,file=str(1:n),status='replace')
        ! header
        write(nu,*)
        write(nu,'("Output parameters")')
        write(nu,*)
        call drawline(nu,1,55)
        write(nu,'(3x,"No.",2x)',advance='no')
        write(nu,'(4x,"Temp",4x)',advance='no')
        write(nu,'(4x,"Time",4x)',advance='no')
        write(nu,'(5x,"DPA",4x)',advance='no')
        write(nu,'(4x,"SDPA",4x)',advance='no')
        write(nu,*)
        call drawline(nu,1,55)
        write(nu,'(2x,i5.5,1x)',advance='no') int(nout)
        write(nu,'(1pe11.5,1x)',advance='no') te
        write(nu,'(1pe11.5,1x)',advance='no') ti
        write(nu,'(1pe11.5,1x)',advance='no') dp
        write(nu,'(1pe11.5,1x)',advance='no') sd
        write(nu,*)
        call drawline(nu,1,55)
        ! print the objects
        call print_sim_obj(nu)
     close(nu)
  end if

  return

end subroutine print_count_obj
